package com.mogujie.tt.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.mogujie.tt.R;
import com.mogujie.tt.ui.base.QZBaseActivity;

/**
 * Created by Administrator on 2015/11/14.
 */
public class MobileOfficeActivity extends QZBaseActivity {

    private WebView webView;

  //  private TextView webViewTitle ;
  //  private TextView webViewBack ;
 //   private TextView webViewMenu ;
    private TextView webViewRetreat ;
    private TextView webViewAdvance;
    private TextView webViewRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater.from(MobileOfficeActivity.this).inflate(R.layout.tt_activity_mobile_office,topContentView);
        showTopInformation("移动办公");

        webView = (WebView) findViewById(R.id.webview);	//WebView对象
        //从上个Activity获取加载的Url
        String webViewLoadUrl=getIntent().getStringExtra("webViewLoadUrl");
        webView.loadUrl(webViewLoadUrl);

        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);			//支持js

        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());

     //   webViewTitle =  (TextView) findViewById(R.id.webview_title);//标题
     //   webViewBack = (TextView) findViewById(R.id.webview_back);//返回按钮
     //   webViewMenu = (TextView) findViewById(R.id.webview_menu);//菜单
        webViewRetreat = (TextView) findViewById(R.id.webview_retreat);//后退
        webViewAdvance = (TextView) findViewById(R.id.webview_advance);//前进
        webViewRefresh = (TextView) findViewById(R.id.webview_refresh);//刷新

      /*  webViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobileOfficeActivity.this.finish(); //返回上一个Activity，也就是关闭当前
            }
        });

        webViewMenu.setVisibility(webViewMenu.INVISIBLE);//暂时隐藏菜单按钮*/

        webViewRetreat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.goBack();   //后退
            }
        });
        webViewAdvance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.goForward();//前进
            }
        });
        webViewRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.reload();  //刷新
            }
        });


    }

    /**
     WebViewClient常用方法：
     doUpdateVisitedHistory 更新历史记录
     onFormResubmission 应用程序重新请求网页数据
     onLoadResource 加载指定地址提供的资源
     onPageFinished 网页加载完毕
     onPageStarted 网页开始加载
     onReceivedError 报告错误信息
     onScaleChangedWebView发生改变
     shouldOverrideUrlLoading 控制新的连接在当前WebView中打开
     */
    public class WebViewClient extends android.webkit.WebViewClient{

        //控制新的连接在当前WebView中打开
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        //网页加载完毕
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        //报告错误信息
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            //super.onReceivedError(view, errorCode, description, failingUrl);
            // webView.loadUrl("file:///android_asset/webview/error.html");
        }
    }

    /**
     WebChromeClient常用方法：
     onCloseWindow 关闭WebView
     onCreateWindow 创建WebView
     onJsAlert 处理Javascript中的Alert对话框
     onJsConfirm处理Javascript中的Confirm对话框
     onJsPrompt处理Javascript中的Prompt对话框
     onProgressChanged 加载进度条改变
     onReceivedlcon 网页图标更改
     onReceivedTitle 网页Title更改
     onRequestFocus WebView显示焦点
     * */
    public class WebChromeClient extends android.webkit.WebChromeClient{

        //网页标题修改
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
         /*   if(title.length()>5)
                webViewTitle.setText(title.substring(0, 5));
            else
                webViewTitle.setText(title);*/
        }
    }

    /**
     * 显示顶部信息
     */
    private void showTopInformation(String test){
        if(test == null){
            return;
        }
        setTopBarDisplay();

        setCenterText(test);
    }


    public void onClickExit(View view){
        finish();
    }

}

